//
//  XSQWelcomeViewController.swift
//  WeiBo
//
//  Created by 许世强 on 2017/3/31.
//  Copyright © 2017年 许世强. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class XSQWelcomeViewController: UIViewController {

    //懒加载背景视图控件
      fileprivate lazy var bgImageView:UIImageView = UIImageView(image:UIImage(named:"ad_background"))
    
    //懒加载头像
    fileprivate lazy var headImageView:UIImageView = {
        
        let imageView = UIImageView()
        
        //通过ViewModel获取头像地址
        if  let imageViewUrl = XSQUserAccountViewModel.shareViewModel.userAcount?.avatar_large
        {
            imageView.sd_setImage(with: URL(string:imageViewUrl),placeholderImage:UIImage(named:"avatar_default_big"))
        }
        
        //设置圆边
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        return imageView
        }()
    
    //懒加载消息
    fileprivate lazy var messageLabel:UILabel = {
    
        let label = UILabel()
        
        if let name = XSQUserAccountViewModel.shareViewModel.userAcount?.name{
            label.text = "你好,\(name),欢迎回来"
        } else {
            
            label.text = "你好,欢迎回来"
        }
        
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.textColor = UIColor.darkGray
        
        return label
        
    }()
    
    //view使用自定义的背景视图
    override func loadView() {
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
      
    }
    
    //添加控件,设置约束
    fileprivate func setupUI(){
        
        view.addSubview(headImageView)
        
        view.addSubview(messageLabel)
        
        headImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width:90,height:90))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
    }
    
    //在视图加载完成后,执行弹簧动画
    override func  viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //开启头像的弹簧效果  和文字的渐显示效果
    fileprivate func startAnimation(){
    
        //先设置文字的透明度为0
        messageLabel.alpha = 0
        
        //更新头像约束
        headImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //动画
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            
            //更新完约束以后,让子控件去调整约束
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animate(withDuration: 1.5, animations: {
                    self.messageLabel.alpha = 1
                },completion:{(_)in
                
                    //发送切换到根视图控制器进入到首页的通知
                    NotificationCenter.default.post(name: (NSNotification.Name(rawValue: SwitchRootVCNotification)), object: nil)
                    
                })
        }
        
    }

    
    
    
}
